import {
  Controller,
  Inject,
  Post,
  Get,
  Put,
  Del,
  Body,
  Param,
  Query
} from "@midwayjs/core";
import { PrizeService } from "../service/prize.service";
import {
  PrizeCreateDto,
  PrizeListQueryDto,
  PrizePageQueryDto,
  PrizeUpdateDto
} from "../dto/prize.dto";
import { Utils } from "../utils";
import { ApiTags } from "@midwayjs/swagger";
import { PrizeWeightUpdateDto } from "../dto/prize.dto";

@ApiTags(["奖品管理"])
@Controller("/auth-api/prize")
export class PrizeController {
  @Inject()
  prizeService: PrizeService;

  @Inject()
  utils: Utils;

  /**
   * 创建奖品
   */
  @Post("/")
  async create(@Body() dto: PrizeCreateDto) {
    const data = await this.prizeService.createPrize(dto);
    return this.utils.response.success({ data });
  }

  /**
   * 奖品列表（不分页）
   */
  @Get("/list")
  async list(@Query() query: PrizeListQueryDto) {
    const data = await this.prizeService.list(query);
    return { success: true, data };
  }

  /**
   * 奖品分页列表
   */
  @Get("/page")
  async page(@Query() query: PrizePageQueryDto) {
    const data = await this.prizeService.page(query);
    return this.utils.response.success({ data });
  }

  /**
   * 奖品详情
   */
  @Get("/:id")
  async info(@Param("id") id: string) {
    const data = await this.prizeService.info(id);
    return { success: !!data, data };
  }

  /**
   * 更新奖品
   */
  @Put("/:id")
  async update(@Param("id") id: string, @Body() dto: PrizeUpdateDto) {
    const success = await this.prizeService.update(id, dto);
    return { success };
  }

  /**
   * 删除奖品（软删除）
   */
  @Del("/:id")
  async remove(@Param("id") id: string) {
    const success = await this.prizeService.remove(id);
    return { success };
  }

  /**
   * 批量更新奖品权重
   */
  @Post("/batch-update-weight")
  async batchUpdateWeight(@Body() dto: PrizeWeightUpdateDto) {
    // 调用 service 层批量更新权重
    const result = await this.prizeService.batchUpdatePrizeListWeight(dto);
    // 返回统一响应结构
    return this.utils.response(result);
  }
}
